package com.feihong.yw189.fragment.shouye;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.AppUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.feihong.yw189.R;
import com.feihong.yw189.activity.buka.BukaActivity;
import com.feihong.yw189.activity.kaika.KaiKaActivity;
import com.feihong.yw189.activity.shouye.KeHuYanZhengActivity;
import com.feihong.yw189.activity.shouye.MoreActivity;
import com.feihong.yw189.activity.shouye.PaiHangActivity;
import com.feihong.yw189.activity.shouye.PhoneRechargeActivity;
import com.feihong.yw189.activity.yonghuzhongxin.wodexiaoxi.SystemNoticeDetailActivity;
import com.feihong.yw189.adapter.HomePageFunMenuAdapter;
import com.feihong.yw189.adapter.SpaceItemDecoration;
import com.feihong.yw189.app.App;
import com.feihong.yw189.bluetooth.startreadcard.SenRuiConnectUtils;
import com.feihong.yw189.bluetooth.utils.util.ConstantsValue;
import com.feihong.yw189.bluetooth.utils.util.PrefUtils;
import com.feihong.yw189.fragment.base.BaseFragment;
import com.feihong.yw189.http.Host;
import com.feihong.yw189.http.HttpTools;
import com.feihong.yw189.listener.http.RequestCallBack;
import com.feihong.yw189.model.CardDataInfo;
import com.feihong.yw189.model.HomeFunList;
import com.feihong.yw189.model.ShouYeModel;
import com.feihong.yw189.model.User.UserEntity;
import com.feihong.yw189.paidan.activity.DispatchActivity;
import com.feihong.yw189.utils.UserLoginInfo;
import com.feihong.yw189.view.MyScrollviewWebview;
import com.feihong.yw189.view.SpacesItemDecoration;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Sabirjan on 2017/11/13.
 * DX189.
 * 飞宏电子
 * 首页
 */

@SuppressLint("ValidFragment")
public class ShouYeFragment extends BaseFragment {

    HomePageFunMenuAdapter homePageFunMenuAdapter;
    GridLayoutManager gridLayoutManager;
    Unbinder unbinder;
    @BindView(R.id.img_btn_servers)
    ImageButton imgBtnServers;
    @BindView(R.id.wbframe)
    WebView mWebview;
    @BindView(R.id.scrollView)
    ScrollView scrollView;
    @BindView(R.id.refresh)
    SwipeRefreshLayout swipeRefreshLayout;


    @BindView(R.id.txt_title)
    ImageView txtTitle;
    @BindView(R.id.title_layout)
    LinearLayout titleLayout;
    @BindView(R.id.iv_user_photo)
    CircleImageView ivUserPhoto;
    @BindView(R.id.tv_username)
    TextView tvUsername;
    @BindView(R.id.tv_user)
    TextView tvUser;
    @BindView(R.id.lay_tongzhi)
    RelativeLayout layTongzhi;
    @BindView(R.id.rl_ywkb)
    RelativeLayout rlYWKB;
    @BindView(R.id.tv_Notice)
    TextView tvNotice;

    @BindView(R.id.rl_headpic)
    RelativeLayout rlHeadPic;

    @BindView(R.id.homePageFunMenuRecyclerView)
    RecyclerView mFunMenuRecyclerView;
    Intent mIntent;
    Callback.Cancelable cancelable;
    Callback.Cancelable shouyeable;
    private String tvNoticeUrl;

    static final List<Integer> HOME_FUN_LIST = new ArrayList<>();

    public static ShouYeFragment newInstance(String tabName) {
        ShouYeFragment homeFragment = new ShouYeFragment();
        homeFragment.tag = tabName;
        // 手机开卡
        HOME_FUN_LIST.add(1);
        // 补卡
        HOME_FUN_LIST.add(2);
        // 客户验证
        HOME_FUN_LIST.add(3);
        // 宽带融合
        HOME_FUN_LIST.add(4);
        // 话费充值
        HOME_FUN_LIST.add(5);
        // 派单业务
        HOME_FUN_LIST.add(6);
        // 预约下单
        HOME_FUN_LIST.add(7);
        // 更多
        HOME_FUN_LIST.add(8);
        return homeFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        fragmentView = inflater.inflate(R.layout.fragment_sy, container, false);
        unbinder = ButterKnife.bind(this, fragmentView);
        initView();
        return fragmentView;
    }

    @Override
    protected void setTheme() {

    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        Log.i(TAG, "onLazyInitView: 加载" );
        //refreshToken();
        //绑定数据
        bindData();
        getCardData();
        getUserInfo();
    }

    //获取用户信息
    private void getUserInfo() {
        //获取用户信息
        App.getApp().getHttpTools().userInfo(new RequestCallBack() {
            @Override
            public void onSuccess(Object result) {
                try {
                    parseJson("" + result);
                } catch (Exception ex) {
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(Callback.CancelledException cex) {
            }

            @Override
            public void onFinished() {

            }
        });
    }

    //用户信息解析
    private void parseJson(String jsonStr) {

        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            String resultCode = jsonObject.optString("status");
            if (resultCode.equals("1")) {
                UserEntity userEntity = new UserEntity();
                JSONObject personInfoData = jsonObject.optJSONObject("data");
                if (personInfoData != null) {
                    userEntity.setUserID(personInfoData.optString("user_id"));
                    userEntity.setUserName(personInfoData.optString("user_name"));
                    userEntity.setUserEmail(personInfoData.optString("email"));
                    userEntity.setUserMobile(personInfoData.optString("mobile"));
                    userEntity.setUserType(personInfoData.optString("user_type"));
                    userEntity.setUserStatus(personInfoData.optString("status"));
                    userEntity.setFaceReadSign(personInfoData.optString("face_read"));
                    userEntity.setTransToAccountPermission(personInfoData.optString("is_zhuanzhang"));
                    userEntity.setBalaceTransOutPermission(personInfoData.optString("is_zhuanchu"));
                    userEntity.setReissueCardPermission(personInfoData.optString("reissue_card"));
                    userEntity.setPlatFormTel(personInfoData.optString("reissue_card"));
                    JSONObject personal = personInfoData.optJSONObject("personal");
                    if (personal != null) {
                        userEntity.setUserIDCardName(personal.optString("real_name"));
                        userEntity.setUserIDCardNum(personal.optString("idcard"));
                        userEntity.setUserSex(personal.optString("sex"));
                        userEntity.setUserPic(personal.optString("user_img"));
                        userEntity.setUserTel(personal.optString("tel"));
                        userEntity.setUsrQQ(personal.optString("qq"));
                        userEntity.setUserAccountManagerName(personal.optString("contact_name"));
                        userEntity.setUserAccountManagerContactTel(personal.optString("contact_tel"));
                        userEntity.setUsrAdr(personal.optString("address"));
                        userEntity.setUserStoreAccount(personal.optString("dj_account"));
                    }
                    JSONObject money = personInfoData.optJSONObject("money");
                    if (money != null) {
                        userEntity.setUserWalletStatus(money.optString("state"));
                        userEntity.setUserAliPayAccount(money.optString("alipay_account"));
                        userEntity.setAliPayRate(money.optString("alipay_rate"));
                    }
                    JSONObject bankInfo = personInfoData.optJSONObject("money_bank");
                    if (bankInfo != null) {
                        userEntity.setUserBankNum(bankInfo.optString("bank_num"));
                        userEntity.setUserBankName(bankInfo.optString("open_bank"));
                        userEntity.setUserBankCardAccountName(bankInfo.optString("open_name"));
                        userEntity.setUserBankRate(bankInfo.optString("bank_rate"));
                    }
                    JSONObject balance = personInfoData.optJSONObject("balance");
                    if (balance != null) {
                        userEntity.setUserAccountBalance(balance.optString("money"));
                        userEntity.setUserFreezingBalance(balance.optString("freeze_money"));
                    }
                    JSONObject verify = personInfoData.optJSONObject("extension");
                    if (verify != null) {
                        userEntity.setUserVerifyMobileSign(verify.optString("is_mobile"));
                        userEntity.setUserVerifyEmailSign(verify.optString("is_email"));
                        userEntity.setUserVerifyRealNameSign(verify.optString("is_user"));
                    }
                    JSONObject region = personInfoData.optJSONObject("region");
                    if (region != null) {
                        userEntity.setUserRegionID(region.optString("parent_id"));
                        userEntity.setUserReginName(region.optString("region_name"));
                    }
                    JSONObject agent = personInfoData.optJSONObject("agent");
                    if (agent != null) {
                        userEntity.setUserAgentUserID(agent.optString("user_id"));
                        userEntity.setUserAgentUserName(agent.optString("user_name"));
                        userEntity.setUserAgentUserEmail(agent.optString("email"));
                    }
                    JSONObject agentP = personInfoData.optJSONObject("agent_p");
                    if (agentP != null) {
                        userEntity.setUserAgentTel(agentP.optString("tel"));
                        userEntity.setUserAgentUserImg(agentP.optString("user_img"));
                    }
                    UserLoginInfo.getUserLoginInfoInstance().saveUserLoginInfo(userEntity);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @SuppressLint({"JavascriptInterface", "NewApi"})
    @Override
    public void initView() {

        //new HomeDialog(_getActivity(),"", "1").show();

        swipeRefreshLayout.setColorSchemeResources(R.color.appBlue);
        scrollView.scrollTo(0, 0);
        scrollView.smoothScrollTo(0, 0);
        mWebview.setOnTouchListener((v, event) -> (event.getAction() == MotionEvent.ACTION_MOVE));
        WebSettings webSettings = mWebview.getSettings();
        mWebview.setWebViewClient(mWebViewClient);
        scrollView.scrollTo(0, 0);
        scrollView.smoothScrollTo(0, 0);
        webSettings.setGeolocationEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setDefaultTextEncodingName("utg-8");
        webSettings.setJavaScriptEnabled(true);
        homePageFunMenuAdapter = new HomePageFunMenuAdapter(_mActivity);

        gridLayoutManager = new GridLayoutManager(_mActivity, 4);

        mFunMenuRecyclerView.setLayoutManager(gridLayoutManager);
        mFunMenuRecyclerView.setAdapter(homePageFunMenuAdapter);
        swipeRefreshLayout.setOnRefreshListener(() -> {
            // 刷新读卡参数
            bindData();
            getCardData();
        });

    }


    private void getCardData() {
        cancelable = App.getApp().getHttpTools().getCardData(new RequestCallBack<String>() {
            @Override
            public void onSuccess(String s) {
                parsereadJson(s);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(Callback.CancelledException cex) {
            }

            @Override
            public void onFinished() {

            }
        });

    }

    private void parsereadJson(String s) {
        try {
            Gson gson = new Gson();
            CardDataInfo cardDataInfo = gson.fromJson(s, CardDataInfo.class);
            if (cardDataInfo != null) {
                if (cardDataInfo.getStatus() == 1) {
                    CardDataInfo.DataBean.ConfigBean.ANDROIDBean android = cardDataInfo.getData().getConfig().getANDROID();
                    if (android != null) {
                        // 恒鸿达
                        PrefUtils.setString(getActivity(), ConstantsValue.HHDserverIp, android.getHHD().getServerIp());
                        PrefUtils.setString(getActivity(), ConstantsValue.HHDaccount, android.getHHD().getAccount());
                        PrefUtils.setString(getActivity(), ConstantsValue.HHDport, android.getHHD().getPort());

                        // 卡尔分体
                        PrefUtils.setString(getActivity(), ConstantsValue.KRFTserverIp, android.getKRFT().getServerIp());
                        PrefUtils.setString(getActivity(), ConstantsValue.KRFTaccount, android.getKRFT().getAccount());
                        PrefUtils.setString(getActivity(), ConstantsValue.KRFTpassword, android.getKRFT().getPassword());
                        PrefUtils.setString(getActivity(), ConstantsValue.KRFTport, android.getKRFT().getPort());


                        // 森锐
                        PrefUtils.setString(getActivity(), ConstantsValue.SRserverIp, android.getSR().getServerIp());
                        PrefUtils.setString(getActivity(), ConstantsValue.SRaccount, android.getSR().getAccount());
                        PrefUtils.setString(getActivity(), ConstantsValue.SRpassword, android.getSR().getPassword());
                        PrefUtils.setString(getActivity(), ConstantsValue.SRport, android.getSR().getPort());
                        PrefUtils.setString(getActivity(), ConstantsValue.SRkey, android.getSR().getKey());

                        // 信通
                        PrefUtils.setString(getActivity(), ConstantsValue.XTserverIp, android.getXT().getServerIp());
                        PrefUtils.setString(getActivity(), ConstantsValue.XTport, android.getXT().getPort());
                    }

                } else {
                    showErrorToast(cardDataInfo.getInfo());
                }
            }
        } catch (Exception e) {
        }
    }

    @Override
    public void bindData() {

        if (shouyeable != null && !shouyeable.isCancelled()) {
            shouyeable.cancel();
        }
        if (cancelable != null && cancelable.isCancelled()) {
            cancelable.cancel();
        }

        getFunMenuList();

        //默认    宽带排行
        String paiHangNumber = "0";
        SpannableString spannableString = new SpannableString("今日宽带" + paiHangNumber + "户");
        int text = paiHangNumber.length();
        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.RED);
        spannableString.setSpan(foregroundColorSpan, 4, 4 + text, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new RelativeSizeSpan(1.5f), 4, 4 + text, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        /*----------------------------*/
        //默认  开卡排行
        String paiHangNum = "0";
        SpannableString spannabletext = new SpannableString("今日开卡" + paiHangNum + "张");
        int textr = paiHangNum.length();
        ForegroundColorSpan ColorSpan = new ForegroundColorSpan(Color.RED);
        spannabletext.setSpan(ColorSpan, 4, 4 + textr, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        spannabletext.setSpan(new RelativeSizeSpan(1.5f), 4, 4 + textr, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        //更新token,防止空指针
        shouyeable = App.getApp().getHttpTools().getShouYe(new RequestCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    ShouYeModel shouYeModelye = JSON.parseObject("" + result, ShouYeModel.class);
                    if (shouYeModelye.getStatus() == 1) {
                        if (shouYeModelye.getData() != null) {
                            // 森瑞需要
                            if (shouYeModelye.getData().getDevice_sn() != null && !TextUtils.isEmpty(shouYeModelye.getData().getDevice_sn())) {
                                PrefUtils.setString(getContext(), SenRuiConnectUtils.SRDEVICESN, shouYeModelye.getData().getDevice_sn().trim());
                            } else {
                                PrefUtils.setString(getContext(), SenRuiConnectUtils.SRDEVICESN, "");
                            }

                            if (shouYeModelye.getData().getYesterday_first() != null) {
                                Log.i(TAG, "onSuccess: ");
                                Glide.with(ShouYeFragment.this).load(shouYeModelye.getData().getYesterday_first().getUser_img()).apply(new RequestOptions().placeholder(R.mipmap.my_setting_photo)).into(ivUserPhoto);
                                tvUsername.setText(shouYeModelye.getData().getYesterday_first().getAgent_name() + "  " + shouYeModelye.getData().getYesterday_first().getUser_name());
                            }
                            if (shouYeModelye.getData().getToday_kuandai() != null) {
                                //宽带排行
                                SpannableString spannableString = new SpannableString("今日宽带" + shouYeModelye.getData().getToday_kuandai().getCount() + "户");
                                int text = shouYeModelye.getData().getToday_kuandai().getCount().length();
                                ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.RED);
                                spannableString.setSpan(foregroundColorSpan, 4, 4 + text, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                                spannableString.setSpan(new RelativeSizeSpan(1.5f), 4, 4 + text, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

                            } else {
                                //默认    宽带排行
                                String paiHangNumber = "0";
                                SpannableString spannableString = new SpannableString("今日宽带" + paiHangNumber + "户");
                                int text = paiHangNumber.length();
                                ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.RED);
                                spannableString.setSpan(foregroundColorSpan, 4, 4 + text, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                                spannableString.setSpan(new RelativeSizeSpan(1.5f), 4, 4 + text, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            }
                            if (shouYeModelye.getData().getToday_kaika() != null) {
                                //开卡排行
                                SpannableString spannabletext = new SpannableString("今日开卡" + shouYeModelye.getData().getToday_kaika().getCount() + "张");
                                int text = shouYeModelye.getData().getToday_kaika().getCount().length();
                                ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.RED);
                                spannabletext.setSpan(foregroundColorSpan, 4, 4 + text, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                                spannabletext.setSpan(new RelativeSizeSpan(1.5f), 4, 4 + text, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            } else {
                                //默认  开卡排行
                                String paiHangNum = "0";
                                SpannableString spannabletext = new SpannableString("今日开卡" + paiHangNum + "张");
                                int text = paiHangNum.length();
                                ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.RED);
                                spannabletext.setSpan(foregroundColorSpan, 4, 4 + text, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                                spannabletext.setSpan(new RelativeSizeSpan(1.5f), 4, 4 + text, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            }
                            if (shouYeModelye.getData().getNotice() != null) {
                                //营维快报
                                tvNotice.setText(shouYeModelye.getData().getNotice().getTitle());
                                tvNoticeUrl = shouYeModelye.getData().getNotice().getUrl();
                            }
                        }
                    } else {
                        Glide.with(getActivity()).load(R.mipmap.my_setting_photo).into(ivUserPhoto);
                        //默认    宽带排行
                        String paiHangNumber = "0";
                        SpannableString spannableString = new SpannableString("今日宽带" + paiHangNumber + "户");
                        int text = paiHangNumber.length();
                        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.RED);
                        spannableString.setSpan(foregroundColorSpan, 4, 4 + text, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                        spannableString.setSpan(new RelativeSizeSpan(1.5f), 4, 4 + text, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

                        //默认  开卡排行
                        String paiHangNum = "0";
                        SpannableString spannabletext = new SpannableString("今日开卡" + paiHangNum + "张");
                        int textr = paiHangNum.length();
                        ForegroundColorSpan ColorSpan = new ForegroundColorSpan(Color.RED);
                        spannabletext.setSpan(ColorSpan, 4, 4 + textr, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                        spannabletext.setSpan(new RelativeSizeSpan(1.5f), 4, 4 + textr, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

                        //营维快报
                        tvNotice.setText("营维快报");
                        tvNoticeUrl = null;
                    }
                } catch (Exception err) {
                    showToast("数据出错了,请重试");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                showToast("请检查网络");
            }

            @Override
            public void onCancelled(Callback.CancelledException cex) {
            }

            @Override
            public void onFinished() {
                // 加载h5
                if (mWebview == null) {
                    return;
                }
                webviewIsSuccess = true;
                String strUrl = HttpTools.WEB_FIST_PAGE + "?token=" + UserLoginInfo.getUserLoginInfoInstance().getUserToken() + "&version=" + "and." + AppUtils.getAppVersionName();
                Log.i(TAG, "onFinished  加载路径: " + strUrl);
                mWebview.loadUrl(strUrl);
                if (swipeRefreshLayout != null) {
                    swipeRefreshLayout.setRefreshing(false);
                }
            }
        });
    }

    /**
     * 获取首页功能列表
     */
    private void getFunMenuList() {

        Log.i(TAG, "getFunMenuList: ");
//        mFunMenuRecyclerView.addItemDecoration(new SpacesItemDecoration(_mActivity));
        homePageFunMenuAdapter.setOnItemClick((homeFunList, position) -> {
            switch (homeFunList.getFunID()) {
                case 1:
                    //手机开卡
                    mIntent = new Intent(getContext(), KaiKaActivity.class);
                    mIntent.putExtra(KaiKaActivity.CREAT_CARD_TYPE_IS_BROADBAND, false);
                    startActivity(mIntent);
                    break;
                case 2:
                    //补卡
                    String reissue_card = UserLoginInfo.getUserLoginInfoInstance().getUserLoc().getReissueCardPermission();
                    if (reissue_card != null) {
                        if (reissue_card.equals("1")) {
                            mIntent = new Intent(getContext(), BukaActivity.class);
                            startActivity(mIntent);
                        } else {
                            showErrorToast("该账号无补卡权限,请联系上级代理.");
                        }
                    }
                    break;
                case 3:
                    //客户验证
                    mIntent = new Intent(getContext(), KeHuYanZhengActivity.class);
                    startActivity(mIntent);
                    break;
                case 4:
                    //宽带融合
                    mIntent = new Intent(getContext(), KaiKaActivity.class);
                    mIntent.putExtra(KaiKaActivity.CREAT_CARD_TYPE_IS_BROADBAND, true);
                    startActivity(mIntent);
                    break;
                case 5:
                    //话费充值
                    mIntent = new Intent(getContext(), PhoneRechargeActivity.class);
                    startActivity(mIntent);
                    break;
                case 6:
                    mIntent = new Intent(getContext(), DispatchActivity.class);
                    startActivity(mIntent);
                    break;
                case 7:
                    // 预约下单
                    mIntent = new Intent(getContext(), SystemNoticeDetailActivity.class);
                    mIntent.putExtra("url", Host.HTTP_HOME + HttpTools.YUYUE_ORDER_URL + "?token=" + UserLoginInfo.getUserLoginInfoInstance().getUserToken());
                    mIntent.putExtra("title", "预约下单");
                    startActivity(mIntent);
                    break;
                case 8:
                    mIntent = new Intent(getContext(), MoreActivity.class);
                    startActivity(mIntent);
                    break;
                default:
                    break;
            }
        });
        App.getApp().getHttpTools().getHomeFunList(new RequestCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    if (!TextUtils.isEmpty(result)) {
                        JSONObject jsonObject = new JSONObject(result);
                        int resultCode = jsonObject.optInt("status");
                        if (resultCode == 1) {
                            JSONArray jsonArray = jsonObject.optJSONArray("data");
                            List<HomeFunList> listList = new ArrayList<>();
                            if (jsonArray != null) {
                                for (int index = 0; index < jsonArray.length(); index++) {
                                    JSONObject funList = jsonArray.optJSONObject(index);
                                    String iconUrl = funList.optString("funicon");
                                    String funName = funList.optString("funname");
                                    int funID = funList.optInt("funid");
                                    // 不信任服务端  如果服务端添加新功能,则客户端需要在本地同步添加功能id,
                                    // 避免服务端自主添加了功能,而客户端没有响应的处理
                                    if (HOME_FUN_LIST.contains(funID)) {
                                        HomeFunList homeFunList = new HomeFunList(iconUrl, funName, funID);
                                        listList.add(homeFunList);
                                    }
                                }
                            }
                            homePageFunMenuAdapter.setData(listList);
                        } else {
                            showToast("服务端：" + jsonObject.optInt("info"));
                        }
                    } else {
                        showToast("服务端数据异常");
                    }
                } catch (JSONException js) {
                    showToast("服务端数据解析异常");
                } catch (Exception e) {

                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(Callback.CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    //Activity里面判断返回键
    public boolean canGoBack() {
        return false;
    }

    @Override
    public boolean onBackPressedSupport() {
        return canGoBack();
    }

    private WebChromeClient mWebChromeClient = new WebChromeClient() {
    };
    private WebViewClient mWebViewClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return true;
        }

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
//            if (webviewIsSuccess && mWebview != null) {
//                mWebview.setVisibility(View.VISIBLE);
//                int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
//                view.measure(w, w);
//            }
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            mWebview.setVisibility(View.GONE);
            webviewIsSuccess = false;
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            mWebview.setVisibility(View.GONE);
            webviewIsSuccess = false;
        }

    };

    private boolean webviewIsSuccess = true;

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }

    @OnClick({R.id.rl_ywkb, R.id.iv_user_photo, R.id.txt_paihangbang})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_user_photo:
            case R.id.txt_paihangbang:
                mIntent = new Intent(getContext(), PaiHangActivity.class);
                startActivity(mIntent);

                break;
            case R.id.rl_ywkb:
                //营维快报
                if (!TextUtils.isEmpty(tvNoticeUrl)) {
                    mIntent = new Intent(getContext(), SystemNoticeDetailActivity.class);
                    mIntent.putExtra("title", "公告详情");
                    mIntent.putExtra("url", Host.HTTP_HOME + tvNoticeUrl + "&token=" + UserLoginInfo.getUserLoginInfoInstance().getUserToken());
                    startActivity(mIntent);
                }
                break;

            default:
                break;
        }
    }

    @Override
    public void onPause() {
        super.onPause();
    }


}
